#!/bin/bash
########################################################################################################
# @mail jiangweihan@kylinos.cn
# @author JiangWH
# @company_name KylinOS
########################################################################################################
# @Scpript 该脚本用于麒麟桌面操作系统版本与序列号不匹配导致源更新异常，解决软件商店无法正常使用的问题；
# 只适用于V10 SP1：2107、2203、2303版本，且修复后系统更新将无法使用，若想恢复系统更新，请查阅脚本帮助信息。
########################################################################################################

# 确认以root用户执行该脚本
USER=$(whoami)
if [ "$USER" != 'root' ];then
    echo "Must use root user run this script!!!"
    exit 0
fi

# 获取系统版本信息
OS=$(grep "PRETTY_NAME=" /etc/os-release)
OSTAG=$(grep "PRETTY_NAME=" /etc/os-release)
if [ "$OSTAG" == 1 ];then
    echo "OS: NONE"
    else
    echo "OS: $OS"
fi

ID=$(grep "KYLIN_RELEASE_ID=" /etc/os-release)
IDTAG=$(grep "KYLIN_RELEASE_ID=" /etc/os-release)
if [ "$IDTAG" == 1 ];then
    echo "ID: NONE"
    else
    echo "ID: $ID"
fi

KERNEL=$(uname -r)

if [ "$OS" != 'PRETTY_NAME="Kylin V10 SP1"' ];then
    echo "This system version is not supported"
    exit 0
fi

#屏蔽系统更新自动修改源文件
STOP_SYS_UPDATE(){
grep "archive1.kylinos.cn" /etc/hosts >&/dev/null
if [ $? == 1 ];then
    echo "127.0.0.1 archive1.kylinos.cn" >> /etc/hosts
	else
	sed -i '/archive1.kylinos.cn/c 127.0.0.1 archive1.kylinos.cn' /etc/hosts
fi
rm -f /var/cache/apt/archives/lock >&/dev/null
rm -f /var/lib/apt/lists/lock >&/dev/null
apt clean all

echo "系统更新已屏蔽"
}

RECOVER_SYS_UPDATE(){
grep "archive1.kylinos.cn" /etc/hosts >&/dev/null
if [ $? == 0 ];then
	sed -i '/archive1.kylinos.cn/d' /etc/hosts
    echo "系统更新已恢复，重新运行检查更新即可继续使用服务序列号获取软件源..."
    else
    echo "系统更新已经生效，无需额外操作，可尝试在系统设置点击检查更新中获取序列号软件源..."
fi
}

#config 2107 source.list
APT_2107(){
cat > /etc/apt/sources.list <<EOF
deb http://archive2.kylinos.cn/deb/kylin/production/KY-V10-SP1-\$(ARCH)/custom/kylin-desktop/V10-SP1-\$(ARCH) default all
deb http://archive2.kylinos.cn/deb/kylin/production/PART-V10-SP1/custom/partner/V10-SP1 default all
EOF
echo "源已修改为2107版本"
}

APT_2107_HWE(){
cat > /etc/apt/sources.list <<EOF
deb http://archive2.kylinos.cn/deb/kylin/production/KY-V10-SP1-\$(ARCH)/custom/kylin-desktop/V10-SP1-\$(ARCH) default all
deb http://archive2.kylinos.cn/deb/kylin/production/PART-V10-SP1/custom/partner/V10-SP1 default all
EOF
echo "源已修改为2107 HWE版本"
}

APT_2107_kirin990(){
cat > /etc/apt/sources.list <<EOF
deb https://archive1.kylinos.cn/kylin/KYLIN-ALL 10.1-pv main restricted universe multiverse 
deb https://archive2.kylinos.cn/deb/kylin/production/PART-10_1-kirin990/custom/partner/10_1-kirin990 default all
EOF
echo "源已修改为2107 kirin990版本"
}

APT_2107_kirin9006c(){
cat > /etc/apt/sources.list <<EOF
deb https://archive.kylinos.cn/kylin/KYLIN-ALL 10.1-kv main restricted universe multiverse 
deb https://archive2.kylinos.cn/deb/kylin/production/PART-10_1-kirin9a0/custom/partner/10_1-kirin9a0 default all
EOF
echo "源已修改为2107 kirin9006c版本"
}

#config 2203 source.list
APT_2203(){
cat > /etc/apt/sources.list <<EOF
deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1 main restricted universe multiverse
deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1-2203-updates main restricted universe multiverse
deb http://archive2.kylinos.cn/deb/kylin/production/PART-V10-SP1/custom/partner/V10-SP1 default all
EOF
echo "源已修改为2203版本"
}

APT_2203_HWE(){
cat > /etc/apt/sources.list <<EOF
deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1 main restricted universe multiverse
deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1-2203-hwe-updates main restricted universe multiverse
deb http://archive2.kylinos.cn/deb/kylin/production/PART-V10-SP1/custom/partner/V10-SP1 default all
EOF
echo "源已修改为2203 HWE版本"
}

APT_2203_kirin990(){
cat > /etc/apt/sources.list <<EOF
deb https://archive.kylinos.cn/kylin/KYLIN-ALL/ 10.1-wayland-2203-updates main universe restricted multiverse
deb https://archive.kylinos.cn/kylin/KYLIN-ALL/ 10.1-kirin990-feature main
deb https://archive2.kylinos.cn/deb/kylin/production/PART-10_1-kirin9a0/custom/partner/10_1-kirin9a0 default all
EOF
echo "源已修改为2203 kirin990版本"
}

APT_2203_kirin9006c(){
cat > /etc/apt/sources.list <<EOF
deb https://archive.kylinos.cn/kylin/KYLIN-ALL/ 10.1-wayland-2203-updates main universe restricted multiverse
deb https://archive.kylinos.cn/kylin/KYLIN-ALL/ 10.1-kirin9006C-feature main
deb https://archive2.kylinos.cn/deb/kylin/production/PART-10_1-kirin9a0/custom/partner/10_1-kirin9a0 default all
EOF
echo "源已修改为2203 kirin9006c版本"
}

#config 2303 source.list
APT_2303(){
cat > /etc/apt/sources.list <<EOF
deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1 main restricted universe multiverse
deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1-2303-updates main restricted universe multiverse
deb http://archive2.kylinos.cn/deb/kylin/production/PART-V10-SP1/custom/partner/V10-SP1 default all
EOF
echo "源已修改为2303版本"
}

APT_2303_HWE(){
cat > /etc/apt/sources.list <<EOF
deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1 main restricted universe multiverse
deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1-2303-updates main restricted universe multiverse
deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1-2303-hwe-updates main restricted universe multiverse
deb http://archive2.kylinos.cn/deb/kylin/production/PART-V10-SP1/custom/partner/V10-SP1 default all
EOF
echo "源已修改为2303 HWE版本"
}

APT_2303_HWE_PP(){
cat > /etc/apt/sources.list <<EOF
deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1 main restricted universe multiverse
deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1-2303-updates main restricted universe multiverse
deb http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1-2303-hwe-pp-updates main restricted universe multiverse
deb http://archive2.kylinos.cn/deb/kylin/production/PART-V10-SP1/custom/partner/V10-SP1 default all
EOF
echo "源已修改为2303 HWE PP版本"
}

#更新apt源
APT_UPDATE(){
    apt clean all >&/dev/null
    apt update >&/tmp/apt-update.log
    echo '软件源更新完成，请重新打开软件商店，尝试安装软件；'
    echo '若仍然报错可尝试请使用"sudo bash kylin-apt-source-repair.sh -2"进一步进行修复,修复过程中若出现确认信息请输入Y回车确认。'
}

main(){
if [ "$IDTAG" == 1 ];then
	echo "系统不受支持或者该系统为2107以前的版本,apt源重置将为2107版本,若系统非2107请执行 "sudo bash kylin-apt-source-repair.sh -r" 恢复系统默认更新"
	STOP_SYS_UPDATE
    ID_NONE
    APT_UPDATE
	elif [ "$ID" == 'KYLIN_RELEASE_ID="2203"' ];then
        STOP_SYS_UPDATE
        ID_2203
        APT_UPDATE
        elif [ "$ID" == 'KYLIN_RELEASE_ID="2303"' ];then
            STOP_SYS_UPDATE
            ID_2303
            APT_UPDATE
        else
            echo "This system version is not supported"
fi
}

ID_2203(){
    case "$KERNEL" in
        5.4.18-*-generic)
        #常规5.4.18-53-generic
            APT_2203
        ;;
        5.10.0-*-generic)
        #常规5.10.0-8-generic
            APT_2203_HWE
        ;;
        4.19.71-*-kr990)
        #常规4.19.71-23-kr990
            APT_2203_kirin990
        ;;
        5.4.96-*-kr9a0)
        #常规5.4.96-11-kr9a0
            APT_2203_kirin9006c
        ;;
        *)
            echo "This system version is not supported"
            exit 0
    esac
}

ID_2303(){
    case "$KERNEL" in
        5.4.18-*-generic)
        #常规5.4.18-85-generic
            APT_2303
        ;;
        5.10.0-*-generic)
        #常规5.10.0-8-generic
            APT_2303_HWE
        ;;
        *)
            echo "This system version is not supported"
            exit 0
    esac
}

ID_NONE(){
    case "$KERNEL" in
        5.4.18-*-generic)
            APT_2107
        ;;
        5.10.0-*-generic)
            APT_2107_HWE
        ;;
        5.4.96-*-kr9a0)
            APT_2107_kirin9006c
        ;;

        4.19.71-*-generic)
            APT_2107_kirin990
        ;;
        *)
            echo "This system version is not supported"
            exit 0
    esac
}

REPAIR_0002(){
    rm -f /var/lib/dpkg/lock
    apt-get install -f
    dpkg --configure -a
}

help(){
    echo "-r 恢复系统更新，重新获取服务序列号的软件源 \
          -2 修复软件商店错误，错误代码#0002 \
          -h 获取帮助信息；不加任何参数将屏蔽系统更新，根据系统版本获取对应软件源 "
}

case "$1" in
    -r)
        RECOVER_SYS_UPDATE
    ;;
    -2)
        REPAIR_0002
    ;;
    -h)
        help
    ;;
    *)
        main
esac
